package com.xian.manage.service.impl;

import java.util.List;
import com.xian.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.xian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.xian.manage.domain.PoemType;
import com.xian.manage.mapper.PoemMapper;
import com.xian.manage.domain.Poem;
import com.xian.manage.service.IPoemService;

/**
 * 诗词管理Service业务层处理
 * 
 * @author Hanys
 * @date 2025-04-07
 */
@Service
public class PoemServiceImpl implements IPoemService 
{
    @Autowired
    private PoemMapper poemMapper;

    /**
     * 查询诗词管理
     * 
     * @param id 诗词管理主键
     * @return 诗词管理
     */
    @Override
    public Poem selectPoemById(Long id)
    {
        return poemMapper.selectPoemById(id);
    }

    /**
     * 查询诗词管理列表
     * 
     * @param poem 诗词管理
     * @return 诗词管理
     */
    @Override
    public List<Poem> selectPoemList(Poem poem)
    {
        return poemMapper.selectPoemList(poem);
    }

    /**
     * 新增诗词管理
     * 
     * @param poem 诗词管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPoem(Poem poem)
    {
        poem.setCreateTime(DateUtils.getNowDate());
        int rows = poemMapper.insertPoem(poem);
        insertPoemType(poem);
        return rows;
    }

    /**
     * 修改诗词管理
     * 
     * @param poem 诗词管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePoem(Poem poem)
    {
        poem.setUpdateTime(DateUtils.getNowDate());
        poemMapper.deletePoemTypeByPoemId(poem.getId());
        insertPoemType(poem);
        return poemMapper.updatePoem(poem);
    }

    /**
     * 批量删除诗词管理
     * 
     * @param ids 需要删除的诗词管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePoemByIds(Long[] ids)
    {
        poemMapper.deletePoemTypeByPoemIds(ids);
        return poemMapper.deletePoemByIds(ids);
    }

    /**
     * 删除诗词管理信息
     * 
     * @param id 诗词管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePoemById(Long id)
    {
        poemMapper.deletePoemTypeByPoemId(id);
        return poemMapper.deletePoemById(id);
    }

    /**
     * 新增诗词类型管理信息
     * 
     * @param poem 诗词管理对象
     */
    public void insertPoemType(Poem poem)
    {
        List<PoemType> poemTypeList = poem.getPoemTypeList();
        Long id = poem.getId();
        if (StringUtils.isNotNull(poemTypeList))
        {
            List<PoemType> list = new ArrayList<PoemType>();
            for (PoemType poemType : poemTypeList)
            {
                poemType.setPoemId(id);
                list.add(poemType);
            }
            if (list.size() > 0)
            {
                poemMapper.batchPoemType(list);
            }
        }
    }
}
